package com.hc.demo.service;

import com.hc.demo.domain.Users;
import com.hc.demo.dto.ResultResponse;
import com.hc.demo.exception.TijianException;
import com.hc.demo.exception.TijianExceptionEnum;
import com.hc.demo.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

@Service
public class UserServicelmpl implements UserService {
    @Autowired
    UsersMapper usersMapper;

    // 1. 测试账号逻辑（开发环境专用）
    String testUserId = "13800138000"; // 测试手机号
    String testPassword = "123456";    // 测试密码

    // 先判断账号是否匹配
    @Override
    public ResultResponse login(Users users){
        ResultResponse resultResponse=new ResultResponse();

        if (testUserId.equals(users.getUserId())) {
            // 账号匹配时，单独判断密码
            if (testPassword.equals(users.getPassword())) {
                // 账号密码都正确：返回登录成功
                Users testUser = new Users();
                testUser.setUserId(testUserId);
                testUser.setPassword(testPassword);
                testUser.setRealName("测试用户");

                resultResponse.setCode(ResultResponse.SUCCESS_CODE);
                resultResponse.setDesc("测试账号登录成功");
                resultResponse.setData(testUser);
                return resultResponse;
            } else {
                // 账号正确但密码错误：抛密码错误异常
                throw new TijianException(
                        TijianExceptionEnum.PASSWORD_IS_WRONG.getEnCode(),
                        "测试账号密码错误（正确密码：123456）" // 明确提示测试密码
                );
            }
        }

        Users user=usersMapper.selectByPrimaryKey(users.getUserId());
        if(user==null){
            //定制异常类型
            //抛出异常：该手机号尚未注册
            //  throw new Exception()
            //需要定制异常类型
            throw new TijianException(TijianExceptionEnum.MOBILE_NOT_REGISTER.getEnCode(),
                    TijianExceptionEnum.MOBILE_NOT_REGISTER.getEnMessage());
        }
        if(!user.getPassword().equals(users.getPassword())){
            //抛出异常，密码错误请重新登录
            throw new TijianException(TijianExceptionEnum.PASSWORD_IS_WRONG.getEnCode(),TijianExceptionEnum.PASSWORD_IS_WRONG.getEnMessage());
        }
        resultResponse=new ResultResponse(ResultResponse.SUCCESS_CODE,ResultResponse.SUCCESS_DESC,user);


        // 登录成功时设置状态
        resultResponse.setCode(ResultResponse.SUCCESS_CODE);
        resultResponse.setDesc(ResultResponse.SUCCESS_DESC);
        resultResponse.setData(user); // 设置用户对象到响应数据中
        user.setPassword("***");//放最后，在返回对象之前
        return resultResponse;
    }
    /**
     * 注册功能
     * @param users
     * @return
     */
    @Override
    public ResultResponse regiter(Users users) {


        Users u=usersMapper.selectByPrimaryKey(users.getUserId());

        if(u!=null){
            throw new TijianException(TijianExceptionEnum.MOBILE_ALREDAY_REGISTER.getEnCode(),
                    TijianExceptionEnum.MOBILE_ALREDAY_REGISTER.getEnMessage());
        }

        usersMapper.insert(users);//落库
        return new ResultResponse(ResultResponse.SUCCESS_CODE,ResultResponse.SUCCESS_DESC);
    }

    @Override
    public ResultResponse validatePhone(String userId) {
        Users users=usersMapper.selectByPrimaryKey(userId);
        if(users==null){
            //throw
            return new ResultResponse(TijianExceptionEnum.MOBILE_NOT_REGISTER.getEnCode(),TijianExceptionEnum.MOBILE_NOT_REGISTER.getEnMessage(),null);
        }else{
            return new ResultResponse(ResultResponse.SUCCESS_CODE,ResultResponse.SUCCESS_DESC,null);
        }

    }



}
